package com.gy.boot.other;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

public class MyGoBang extends JFrame {

    PicJPanel pj;

    int cell = 20; // 行列数
    int size = 20; // 每列大小
    int map[][] = new int [cell][cell]; // 棋盘
    int flag = 2; // 1为白, 2为黑, 黑白交齐, 默认为黑
    int winner = 0; // 胜利者


    public static void main(String[] args) {
        new MyGoBang();
    }

    void setMap(int[][] map) {
        this.map = map;
    }
    private MyGoBang() {
        init();
    }
    private void init() { // 逻辑代码
        int width = this.size * this.cell;
        this.setSize(width+5, width+28); // 设置窗体大小
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // 设置窗体关闭时主线程关闭
        this.setLocationRelativeTo(null); // 设置窗体位置为正中间
        this.setResizable(false); // 窗口固定大小
        this.setLayout(null);
        this.setTitle("五子棋");

        // 添加画板
        pj = new PicJPanel(this);
        pj.addMouseListener(new MyMouse(this)); // 监听事件
        this.add(pj);

        this.setVisible(true);

    }
}

class PicJPanel extends JPanel {
    private MyGoBang mj;

    PicJPanel(MyGoBang mj) {
        this.setBackground(Color.ORANGE);
        this.setBounds(0, 0, mj.getWidth(), mj.getHeight());
        this.mj = mj;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        int size = mj.size;
        int cell = mj.cell;
        int map[][] = mj.map;
        for (int i = 0; i < cell; i++) {
            for (int j = 0; j < cell; j++) {
                int x = size * i;
                int y = size * j;
                g.drawRect(x, y, size, size);
                // 1.白棋
                if (map[i][j] == 1) {
                    g.setColor(Color.WHITE);
                    g.fillOval(x, y, size, size);
                }
                // 2.黑棋
                if (map[i][j] == 2) {
                    g.setColor(Color.BLACK);
                    g.fillOval(x, y, size, size);
                }
                g.setColor(Color.BLACK);
            }
        }
    }
}

class MyMouse extends MouseAdapter {
    private MyGoBang mj;

    MyMouse(MyGoBang mj) {
        super();
        this.mj = mj;
    }

    /** 点击事件逻辑 */
    @Override
    public void mousePressed(MouseEvent e) {
        if (mj.winner != 0) {
            // 已经有了胜利者, 重置棋盘
            System.out.println("已胜");
            return;
        }
        // 落子位置
        Point point = e.getPoint();
        int x = (int) point.getX() / mj.size;
        int y = (int) point.getY() / mj.size;
        System.out.println("x = " + x + ", y = " + y);
        // 判断是否已落子
        int map[][] = mj.map;
        if (map[x][y] > 0) {
            System.out.println("已落子");
            return;
        }
        int flag = mj.flag;
        // 设置棋子
        map[x][y] = flag;
        mj.setMap(map);
        // 改变棋子颜色
        mj.flag = flag == 1 ? 2 : 1;
        // 渲染画布
        mj.pj.repaint();
        // 判断是否胜利;
        boolean isWin = isWin(map, x, y, flag);
        if (isWin) {
            mj.winner = flag;
            String msg = flag == 1 ? "白棋胜!!!" : "黑棋胜!!!";
            JOptionPane.showConfirmDialog(mj, msg);
        }
    }

    /** 判断是否相连 */
    private boolean isWin(int map[][], int x, int y, int flag) {
        // 1.横
        boolean winH = isJoinIndex(map, x, y, flag, 1);
        if (winH) {
            return true;
        }
        // 2.竖
        boolean winS = isJoinIndex(map, x, y, flag, 2);
        if (winS) {
            return true;
        }
        // 3. 左斜
        boolean winZX = isJoinIndex(map, x, y, flag, 3);
        if (winZX) {
            return true;
        }
        // 4. 右斜
        return isJoinIndex(map, x, y, flag, 4);
    }
    /** type: 1横, 2竖, 3左斜, 4右斜  */
    private boolean isJoinIndex(int[][] map, int x, int y, int flag, int type) {
        int len = map.length;
        ArrayList<int[]> list;
        if (type == 1 || type == 2) {
            list = listHS(x, y, len, type==1);
        } else {
            list = listX(x, y, len, type==3);
        }
        int num = 0;
        for (int[] index : list) {
            int xi = index[0];
            int yi = index[1];
            int cell = map[xi][yi];
            if (cell == flag) {
                num++;
                if (num >= 5) {
                    return true;
                }
            } else {
                num = 0;
            }
        }
        return false;
    }
    /** 横,竖 判断列表  */
    private ArrayList<int[]> listHS(int x, int y, int len, boolean isH) {
        ArrayList<int[]> list = new ArrayList<>();
        int t = isH ? x : y;
        int min = (t - 4) > 0 ? (t - 4) : 0;
        int max = (t + 5) < len ? (t + 5) : len;
        for (int i = min; i < max; i++) {
            int x2 = isH ? i : x;
            int y2 = isH ? y : i;
            list.add(new int[]{x2, y2});
        }
        return list;
    }
    /** 斜 判断列表 */
    private ArrayList<int[]> listX(int x, int y, int len, boolean isLeft) {
        ArrayList<int[]> list = new ArrayList<>();
        int min = (x - 4) > 0 ? (x - 4) : 0;
        int max = (x + 5) < len ? (x + 5) : len;
        int c = isLeft ? (x + y) : (y - x);
        for (int i = min; i < max; i++) {
            int index = isLeft ? (c - i) : (i + c);
            if (index < 0) {
                continue;
            }
            if (index >= len) {
                break;
            }
            list.add(new int[]{i, index});
        }
        return list;
    }
}